<?php
namespace api\modules\v1\controllers;
use api\common\controllers\UserController as U;
use api\modules\v1\models\Like;
use api\modules\v1\models\News;
use yii\web\HttpException;
use yii\web\NotFoundHttpException;
use Yii;
class LikeController extends U
{
    public $modelClass = 'api\modules\v1\models\Like';


    public function behaviors() {
        if(in_array(Yii::$app->controller->action->id,['contentlist','commentlist'])){ //排除评论列表权限验证
            $this->_is_auth = false;
        }
        return parent::behaviors();
    }

    public function actions()
    {
        $actions = parent::actions();
        unset($actions['index'],$actions['update'], $actions['create'], $actions['delete'], $actions['view']); //卸载操作
        return $actions;
    }

    /*
     * 内容点赞
     * @param $id
     * @param $type  文章类型 1:新闻 2:视频
     */
    public function actionContent(){
        $content_id = (int)Yii::$app->request->post('content_id');
        $catid = (int)Yii::$app->request->post('catid');
        if(empty($content_id)) throw new HttpException(401,'参数content_id不能为空',0);
        if(empty($catid)) throw new HttpException(401,'参数catid不能为空',0);
        $model = new $this->modelClass();
        if($model->likeContent($content_id,$catid)){
            $model->PushTj($content_id,$catid,1);
            return [
                "message"=>"点赞成功!",
            ];
        }
    }

    /*
     * 内容点赞撤销
     * @param $id
     * @param $type  文章类型 1:新闻 2:视频
     */
    public function actionContentreset(){
        $content_id = (int)Yii::$app->request->post('content_id');
        $catid = (int)Yii::$app->request->post('catid');
        if(empty($content_id)) throw new HttpException(401,'参数content_id不能为空',0);
        if(empty($catid)) throw new HttpException(401,'参数catid不能为空',0);
        $model = new $this->modelClass();
        if($model->likeContentreset($content_id,$catid)){
            $model->PushTj($content_id,$catid,0);
            return [
                "message"=>"撤销点赞成功!",
            ];
        }
    }

    /*
     * 评论点赞
     * @param $id
     */
    public function actionComment(){
        $content_id = (int)Yii::$app->request->post('content_id');
        $catid = (int)Yii::$app->request->post('catid');
        $comment_id = (int)Yii::$app->request->post('comment_id');
        if(empty($content_id)) throw new HttpException(401,'参数content_id不能为空',0);
        if(empty($catid)) throw new HttpException(401,'参数catid不能为空',0);
        if(empty($comment_id)) throw new HttpException(401,'参数comment_id不能为空',0);
        $model = new $this->modelClass();
        if($model->likeComment($content_id,$catid,$comment_id)){
            return [
                "message"=>"点赞成功!",
            ];
        }
    }

    /*
     * 评论点赞撤销
     * @param $id
     */
    public function actionCommentreset(){
        $content_id = (int)Yii::$app->request->post('content_id');
        $catid = (int)Yii::$app->request->post('catid');
        $comment_id = (int)Yii::$app->request->post('comment_id');
        if(empty($content_id)) throw new HttpException(401,'参数content_id不能为空',0);
        if(empty($catid)) throw new HttpException(401,'参数catid不能为空',0);
        if(empty($comment_id)) throw new HttpException(401,'参数comment_id不能为空',0);
        $model = new $this->modelClass();
        if($model->likeCommentreset($content_id,$catid,$comment_id)){
            return [
                "message"=>"撤销点赞成功!",
            ];
        }
    }

    /*
     * 点赞文章列表
     * @param $content_id
     * @param $catid
     */
    public function actionContentlist($content_id,$catid){
        $model = new $this->modelClass();
        return $model->Contentlist($content_id,$catid);
    }

    /*
     * 点赞评论列表
     * @param $content_id
     * @param $catid
     */
    public function actionCommentlist($content_id,$comment_id,$catid){
        $model = new $this->modelClass();
        return $model->Commentlist($content_id,$comment_id,$catid);
    }
}